package com.example.myblog1_0.mapper;

import com.example.myblog1_0.model.MyUser;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

/**
 * @Author: xc
 * @Date: 2018/12/14 16:22
 * @Description:
 **/
@Mapper
@Repository
public interface UserMapper {
        /**
         * 由前端传来的用户对象在数据库进行查询
         * @param myUser 传递来的对象
         * @return 查找到的用户封装为一个对象
         */
        @Select("SELECT * FROM user WHERE username=#{username} and password=#{password} ")
        MyUser findUserInfo(MyUser myUser);

        /**
         * 根据用户名查找用户，以检验用户名是否存在
         * @param username 用户名
         * @return 用户对象
         */
        @Select("SELECT * FROM user WHERE username=#{username} ")
        MyUser findUserInfoByName(String username);

        /**
         * 插入一个用户
         * @param myUser 传递来的用户对象
         * @return 插入成功则为true
         */
        @Insert("insert into user(username, password, role,nickname,openid,avatarurl) values(#{username}, #{password}, #{role}, #{nickname},#{openid},#{avatarUrl})")
        boolean insetUser(MyUser myUser);

        /**
         * 根据回调的openid查询用户
         * @param openid 返回的openid
         * @return 用户对象
         */
        @Select("SELECT * FROM user WHERE openid = #{openid}")
        MyUser findUserInfoByOpenid(String openid);

        /**
         * 更新qq账户密码
         * @param myUser
         * @return 更细成功为true
         */
        @Update("update user set username= #{username},password= #{password} where openid = #{openid}")
        boolean updateQqUser(MyUser myUser);

        @Update("update user set nickname= #{nickname} where username = #{username}")
        boolean updateNickname(@Param("username") String username,@Param("nickname") String nickname);

        @Update("update user set avatarurl= #{avatarurl} where username = #{username}")
        boolean updateAvatar(@Param("username") String username,@Param("avatarurl") String url);

        @Select("select * from user where nickname = #{nickname}")
        MyUser findUserByNickname(String nickname);
}
